<?php

defined('SYSPATH') or die('Access error!');

class Model_Trade_Post extends Model
{

    public $rules = array(
        'name' => array
            (
            'not_empty' => null,
        ),
        'content' => array(
            'not_empty' => null,
        )
    );

    /**
     *  插入供求和附件的关联关系
     * @param type $goods_id
     * @param type $attachment_id
     */
    public function add_trade_attachment($trade_id, $attachment_id)
    {
        $sql = "INSERT " . $this->db->get_table('trade_post_attachment') . " SET post_id=" . $trade_id . ",attachment_id=" . $attachment_id;
        $this->db->query($sql);
    }

    /**
     *  通用的查询供求列表(包含分页。如果不需要分页在获取的数据中只要保留下标为list的数据)
     * @param type $where_sql
     * @param type $nums
     * @return type
     */
    public function get_trade_list($where_sql = '', $nums = 10, $order_sql = '')
    {
        if (Arr::validate_arr($where_sql))
        {
            $where_sql = $this->get_table_where('tp', $where_sql);
        }

        if (empty($order_sql))
        {
            $where_sql .= " ORDER BY tp.id DESC";
        }
        else
        {
            $where_sql .= $this->order_by($order_sql, 'tp');
            $where_sql .= " ,tp.id DESC";
        }

        $sql = 'SELECT tp.id,tp.limit_time,tp.view_count,tp.account,tp.comment_count,tp.name,tp.content,tp.post_type,tp.meta_description,tp.add_time,tp.user_id,tp.meta_keywords,u.username,u.avatar ' .
                'FROM ' . $this->table . ' AS tp ' .
                'LEFT JOIN ' . $this->db->get_table('user') . ' AS u ON u.id = tp.user_id ' . $where_sql;
        $data = $this->get_page_list($sql, $nums);
        $trade_temp = $data['list'];
        $trade = array();
        if (Arr::validate_arr($trade_temp))
        {
            foreach ($trade_temp as $value)
            {
                $value['floortime'] = Date::timeformat($value['add_time'], 2);
                $value['content'] = Text::limit_chars(strip_tags($value['content']),100);
                $value['avatar_path'] = Model::factory('user')->create_avatar_path($value['user_id'], $value['avatar']);
                $value['url'] = $this->create_trade_detail_url($value['id']);
                $trade[] = $value;
            }
        }
        $data['list'] = $trade;
        return $data;
    }

    /**
     *  根据供求的id生成详细页面的连接
     * @param type $trade_id
     * @return type
     */
    public function create_trade_detail_url($trade_id)
    {
        return URL::site('trade/detail/' . $trade_id);
    }

    public function create_trade_list_url($category_id = '')
    {
        return URL::site('trade/list/' . $category_id);
    }

    /**
     * 根据问答的id获取详细信息
     * @param type $id
     * @return type
     */
    public function get_trade_detail($id)
    {
        $sql = 'SELECT tp.*,u.username,u.id as user_id,u.avatar,c.name as category_name ,c.id as category_id,a.is_image,a.id as attachment_id,a.file_name ' .
                'FROM ' . $this->table . ' AS tp ' .
                'LEFT JOIN ' . $this->db->get_table('trade_post_attachment') . ' AS tpa ON tpa.post_id = tp.id ' .
                'LEFT JOIN ' . $this->db->get_table('attachment') . ' AS a ON a.id = tpa.attachment_id ' .
                'LEFT JOIN ' . $this->db->get_table('user') . ' AS u ON u.id = tp.user_id ' .
                'LEFT JOIN ' . $this->db->get_table('category') . ' AS c ON c.id = tp.category_id ' .
                "WHERE tp.id = " . $id;
        $post_detail = $this->db->get_row($sql, ARRAY_A);

        if (Arr::validate_arr($post_detail))
        {
            if (!empty($post_detail['attachment_id']))
            {
                $post_detail['attachment_path'] = Model::factory('attachment')->create_attachment_url($post_detail['attachment_id']);
            }
            $post_detail['avatar_path'] = Model::factory('user')->create_avatar_path($post_detail['user_id'], $post_detail['avatar']);
        }
        return $post_detail;
    }

    public function delete_trade($id)
    {
        //删除留言
        Model::factory('trade_comments')->delete(array('trade_id' => $id));
        //删除附件
        $sql = "SELECT attachment_id FROM " . $this->db->get_table('trade_post_attachment') . " WHERE post_id=" . $id;
        $attachment_id = $this->db->get_var($sql);
        if (!empty($attachment_id))
        {
            Model::factory('attachment')->delete_attachment($attachment_id);
            //删除附件和供求的供应关系
            $sql = "DELETE FROM " . $this->db->get_table('trade_post_attachment') . " WHERE post_id=" . $id;
        }
        return $this->delete(array('id' => $id));
    }

}